var cpe_test=sessionStorage.getItem("cpe_id");
var design;
var regions;
var q;


// percentage graph
function designgraph_region(q){
	        
    	cpe_test=sessionStorage.getItem("cpe_id");
    	
		if(cpe_test==undefined)
			document.getElementById('esconde').style.display = 'none';
    //alert(cpe_test);

	    if(q==undefined) {
			q = sessionStorage.getItem("query");
			//alert("undefined is now " + q);
		}
		else {
			sessionStorage.setItem("query",q);
			//alert("setting q " + q);
		}


    $.get( "charts/region_chart.php?query="+q, function( data ) {

        var out = JSON.parse(data);
        var regions = out[0];
        var vodafoneseries = jQuery.parseJSON('['+out[1]+']');
		var ptseries = jQuery.parseJSON('['+out[2]+']');
		var nosseries = jQuery.parseJSON('['+out[3]+']');
		var outrosseries = jQuery.parseJSON('['+out[4]+']');


       // alert(regions) ;

		$(function () {
		    Highcharts.setOptions({ colors: ['#ff2424', '#8fbeff', '#212121', '#A3FF47']});
		    
		    $('#container').highcharts({
		        chart: {
		            type: 'column'
		        },
		        title: {
		            text: ''
		        },
		        xAxis: {
		            categories: regions
		        },
		        yAxis: {
		            min: 0,
		            pointFormat: 'point.percentage',
		            title: {
		                text: 'Percentage'
		            }
		        },
		        tooltip: {
		            pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>',
		            shared: true,
		            useHTML: true
		        },
		        plotOptions: {
		            column: {
		                stacking: 'percent'
		            }
		        },
		        series: [{
		            name: 'Vodafone',
		            data: vodafoneseries
		        }, {
		            name: 'PT',
		            data: ptseries
		        }, {
		            name: 'NOS',
		            data: nosseries
		        }, {
		            name: 'Others',
		            data: outrosseries,
	   				visible: false
		        }] 


		    });
		});
	});

	design = setTimeout("designgraph_region()", 60000);
}

function clearfunc() {
	clearTimeout(design);
	//alert("clearing") ;
}

function setcpe(){
	document.getElementById("cpe_label").innerHTML = "<b>MAC: </b> " + sessionStorage.getItem('cpe_mac') + " <b>Region: </b> " + sessionStorage.getItem('cpe_city');
}